package com.egoo.ticket.server.service.impl;

import com.egoo.ticket.server.dao.EwsDepartmentDao;
import com.egoo.ticket.server.pojo.vo.EwsDepartmentVo;
import com.egoo.ticket.server.pojo.vo.InteractiveDataVo;
import com.egoo.ticket.server.service.DepartmentService;
import com.egoo.ticket.server.utils.common.TreeDataHandleUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Donny_dong
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class DepartmentServiceImpl implements DepartmentService {
	@Autowired
	EwsDepartmentDao ewsDepartmentDao;
	@Autowired
	TreeDataHandleUtil treeDataHandleUtil;

	/**
	 * TODO 获取树型结构机构数据
	 * @author: victor_tang
	 * @createtime: 2020/1/17 13:41
	 * @param
	 * @return: java.utils.List<com.egoo.ews.service.vo.InteractiveData>
	 */
	@Override
	public List<InteractiveDataVo> getDepartmentTreeData(String tenantId) {

		List<InteractiveDataVo> rootList = ewsDepartmentDao.selectDepartmentTreeList(tenantId);
		// 转化数据
		return treeDataHandleUtil.transInteractiveToTreeData(rootList);
	}
	/**
	 * TODO 根据部门ids获取部门名称组合
	 * @author: victor_tang
	 * @createtime: 2020/2/7 12:17
	 * @param deptIds 1
	 * @return: java.lang.String
	 */
	@Override
	public String getNamesByDeptIds(List<String> deptIds,String tenantId) {
		if (deptIds==null || deptIds.size()==0) {
			return null;
		}
		StringBuffer deptNames = new StringBuffer();
		Map<String, Object> searchmap = new HashMap<>(4);
		searchmap.put("deptIds",deptIds);
		searchmap.put("tenantId",tenantId);
		List<EwsDepartmentVo> ewsDepartmentVos = ewsDepartmentDao.selectDepartmentListByCondition(searchmap);
		for (EwsDepartmentVo ewsDepartmentVo : ewsDepartmentVos) {
			deptNames.append(ewsDepartmentVo.getName()).append(",");
		}
		// 删除末尾逗号
		if (deptNames!=null && deptNames.length()>0) {

			deptNames.deleteCharAt(deptNames.length()-1);
			return deptNames.toString();
		}
		return null;
	}
}
